#include "msg_com.h"
#include <unistd.h>
#include <time.h>
#include <pthread.h>
#include "ui_main.h"

MsgCenterHandle_t *handle = NULL;

#define TOPIC_1 "test"
#define TOPIC_2 "test1"
#define TOPIC_3 "test2"
#define TOPIC_4 "test3"

void HandleEvent1(char *event_name, void *data, int srcId)
{
   MSG_CENTER_LOG("event_name: %s data: %s srcId: %d \r\n", event_name, (char *)data, srcId);
}


void *task1(void *usrdata)
{
     int cnt = 0;
     SubscriberEvent(handle, TOPIC_1, HandleEvent1, 0);
	 SubscriberEvent(handle, TOPIC_1, HandleEvent1, 0);
	 SubscriberEvent(handle, TOPIC_2, HandleEvent1, 1);
	 SubscriberEvent(handle, TOPIC_3, HandleEvent1, 2);
	 SubscriberEvent(handle, TOPIC_4, HandleEvent1, ANY_ID);
	 while(1)
	 	{
	 	  
          sleep(1);
		  
		  cnt++;
		  if(cnt == 1)
		  	{
              ReleaseSubscriberEvent(handle, TOPIC_4, HandleEvent1);
		  }
		  else if(cnt == 2)
		  	{
              ReleaseSubscriberEvent(handle, TOPIC_3, HandleEvent1);
		  }
		   else if(cnt == 3)
		  	{
              ReleaseSubscriberEvent(handle, TOPIC_2, HandleEvent1);
		  }
		    else if(cnt == 4)
		  	{
              ReleaseSubscriberEvent(handle, TOPIC_1, HandleEvent1);
		  }
		   else if(cnt == 5)
		  	{
             SubscriberEvent(handle, TOPIC_1, HandleEvent1, 0);
		   	}
		    else if(cnt == 6)
		  	{
            SubscriberEvent(handle, TOPIC_2, HandleEvent1, 1);
		   	}
			 else if(cnt == 7)
		  	{
            SubscriberEvent(handle, TOPIC_3, HandleEvent1, 2);
		   	}

			  else if(cnt == 8)
		  	{
            SubscriberEvent(handle, TOPIC_4, HandleEvent1, ANY_ID);
		   	}
			else if(cnt == 10)
			  	{
                  // MsgCenterDeinit(&handle);
			 }
	 }
}

void *task2(void *usrdata)
{
     while(1)
	 	{
          PublishEvent(handle, TOPIC_1, "qwert", 5, 0);
		  PublishEvent(handle, TOPIC_2, "qwert1", 6, 1);
		  PublishEvent(handle, TOPIC_3, "qwert2", 6, 2);
		  PublishEvent(handle, TOPIC_4, "qwert3", 6, 6); 
		  PublishEvent(handle, TOPIC_4, "qwert3", 6, 7); 
          sleep(1);
	 }
}

void *task3(void *usrdata)
{
	  while(1)
		   {
			 PublishEvent(handle, TOPIC_1, "qwert", 5, 0);
			 PublishEvent(handle, TOPIC_2, "qwert1", 6, 1);
			 PublishEvent(handle, TOPIC_3, "qwert2", 6, 2);
			 PublishEvent(handle, TOPIC_4, "qwert3", 6, 6); 
			 PublishEvent(handle, TOPIC_4, "qwert3", 6, 7); 
			 sleep(2);
		}


}

void *task4(void *usrdata)
{
	  while(1)
		   {
			  SubscriberEvent(handle, TOPIC_1, HandleEvent1, 0);
			 SubscriberEvent(handle, TOPIC_1, HandleEvent1, 0);
			 SubscriberEvent(handle, TOPIC_2, HandleEvent1, 1);
			 SubscriberEvent(handle, TOPIC_3, HandleEvent1, 2);
			 SubscriberEvent(handle, TOPIC_4, HandleEvent1, ANY_ID);
			 sleep(3);
		}


}

pthread_mutex_t       mutexSubscriber;
pthread_mutex_t       mutexPublish;
void mutex_init()
{
    pthread_mutex_init(&mutexSubscriber, NULL);
    pthread_mutex_init(&mutexPublish, NULL);

}

void mutex_sub(bool lock)
{
     if(lock)
     {
        pthread_mutex_lock(&mutexSubscriber);
     }
     else
     {
        pthread_mutex_unlock(&mutexSubscriber);
     }

}

void mutex_pub(bool lock)
{
     if(lock)
     {
        pthread_mutex_lock(&mutexPublish);
     }
     else
     {
        pthread_mutex_unlock(&mutexPublish);
     }
}

int main(void)
{
 
//    handle = MsgCenterInit(mutex_sub, mutex_pub);
//    if(handle == NULL)
//    	{
//    	   MSG_CENTER_LOG("msg center init fail\r\n");
//        return 0;
//    }
//    #if 1
//    pthread_t pt_id;
//    pthread_t pt_id1;
//    pthread_t pt_id2;
//    pthread_t pt_id3;
//    pthread_create(&pt_id, NULL, task1, NULL);
//    pthread_detach(pt_id);
//    pthread_create(&pt_id1, NULL, task2, NULL);
//    pthread_detach(pt_id1);
//    pthread_create(&pt_id2, NULL, task3, NULL);
//    pthread_detach(pt_id2);
//    pthread_create(&pt_id3, NULL, task4, NULL);
//    pthread_detach(pt_id3);
//    #endif
//    while(1)
//    {
//       MsgCenterLoop(handle);
//       usleep(20*1000);
//    }

   ui_app_init();

   while(1)
   {
     ui_app_loop();
     usleep(4*1000);
   }
   return 0;
}